<?php
/**
 * 系统类
 * @author chenlisong E-mail:chenlisong1021@163.com 
 * @version 创建时间：2016年8月8日 上午11:53:43 
 * 类说明
 */
class SystemAction extends BaseAction{
	
	public function index(){
		echo $this->error("非法操作！");
	}
	
	/**
	 * 客户端版本更新
	 * Enter description here ...
	 */
	public function updateClient(){
		$param = $this->param;
		$version = $param['versionName'];
		if($param['type'] == 1){//android
			$newVersion = C('APP_ANDROID_VERSIONNAME');
			$result = $this->compareVersion($version, $newVersion);
			if($result['needUpdate'] == 1){//必须更新
				$result['updateInfo'] = array(C('APP_ANDROID_TITLE'),C('APP_ANDROID_FEATURE'));
				$result['versionName'] = $newVersion;
				$result['updateUrl'] = C('APP_ANDROID_UPDATE_URL');
			}
		}elseif ($param['type']==2){//ios
			$newVersion = C('APP_IOS_VERSIONNAME');
			$result = $this->compareVersion($version, $newVersion);
			if($result['needUpdate'] == 1){//必须更新
				$result['updateInfo'] = array(C('APP_IOS_TITLE'),C('APP_IOS_FEATURE'));
				$result['versionName'] = $newVersion;
				$result['updateUrl'] = "";
			}
		}
		echo $this->suc($result);
	}
	
	
	/**
	 *  版本号比较
	 *  @param $v1 新版本号
	 *  @param $v2 旧版本号
	 *  return boolean
	 */
	private function compareVersion($version,$newVersion){
		$result = array('needUpdate'=>0,'mustUpdate'=>0);
		$param1  = explode('.',$version);
		$param2  = explode('.',$newVersion);
		if($param1[0] == $param2[0] && $param1[1] == $param2[1]){//第一位和第二位只要不同强制更新
			if($param1[2] != $param2[2]){//第三位不同，选择更新
				$result['needUpdate'] = 1;
				$result['mustUpdate'] = 0;
			}
		}else{
			$result['needUpdate'] = 1;
			$result['mustUpdate'] = 1;
		}
		//如果当前版本号大于等于系统版本号，则不更新
		if(version_compare($version, $newVersion,'>=')){
			$result['needUpdate'] = 0;
			$result['mustUpdate'] = 0;
		}
		return $result;
	}
	

	/**
	 * 首页信息
	 * Enter description here ...
	 */
	public function appIndex(){
		$Banner = M('Banner');$Meetingplace = M('Meetingplace');$MeetingplaceProduct = M('MeetingplaceProduct');$Product = M('Product');$Type = M('Type');$ShopAttributeValues = M("ShopAttributeValues");$ShopProduct = M('ShopProduct');
		$param = $this->param;
		if(isset($param['shop_id']) && $param['shop_id']){
			$shop_id = $param['shop_id'];
		}else{
			$shop_id = 0;
		}
		$data['bannerList'] = (array) $Banner->where('flag = 1 and is_on = 1 and type != 5')->field('id,title,img_app as img,type,url')->order('seq asc')->select();
		//活动会场（可有可无，有且仅有一个）取banner图
		$data['hdMeeting'] = $Banner->where('flag = 1 and is_on = 1 and type = 5')->field('url as id,title,img_app as img')->find();
		//icon
		$typeArr = C('SYSTEM_ICON_TYPE_ID');
		$tmap['flag'] = 1;
		$tmap['reserve'] = 1;
		$tmap['id'] = array('in',$typeArr);
		$iconArr = $Type->where($tmap)->field('id as typeId,type_name as typeName,icon')->order("FIELD(id,".implode(',', $typeArr).")")->select();
		$data['iconList'] = $iconArr;
		//特价疯抢（有且仅有一个）
		$map2['flag'] = 1;
		$map2['type'] = 3;
		$map2['is_on'] = 1;
		$id = $Meetingplace->where($map2)->getField('id');
		//$productIds = $MeetingplaceProduct->where('meetingplace_id = '.$id.' AND shop_id='.$shop_id)->getField('GROUP_CONCAT(product_id)');
		$map3['b.flag'] = 1;
		$map3['b.is_on'] = 1;
		$map3['a.meetingplace_id'] = $id;
		$map3['a.shop_id'] = $shop_id;
		$map3['c.shop_id'] = $shop_id;
		$data['tjMeetingId'] = $id;
		//$data['tjMeetingProductList'] = (array) $Product->where($map3)->field('id,name,photo,price,spec,num,is_attr')->order("seq desc")->select();
		$data['tjMeetingProductList'] = (array) $MeetingplaceProduct->alias('a')->join('left join __SHOP_PRODUCT__ as c on c.product_id=a.product_id and c.flag=1 left join __PRODUCT__ as b on c.product_id = b.id and b.flag=1')->field('b.id,b.name,b.photo,CASE WHEN  c.shop_price>0 THEN  c.shop_price ELSE b.price END AS price,b.spec,c.num,b.is_attr,b.tag_type')->where($map3)->limit(0,6)->order("a.seq asc")->select();
		foreach($data['tjMeetingProductList'] as $p_key=>$p_v){
			if($p_v['is_attr']==1){
				$attr_where['product_id'] = $p_v['id'];
				$attr_where['flag'] = 1;
				if(isset($param['shop_id'])){
					$attr_where['shop_id'] = $param['shop_id'];
				}
				$attribute_values = $ShopAttributeValues->field("id,attr_id,attr_name,attr_value_name,is_default,num")->where($attr_where)->order('seq asc')->select();
				if(!empty($attribute_values)){
					$data['tjMeetingProductList'][$p_key]['attribute_values'] = $attribute_values;
					$attr_num = 0;
					foreach($attribute_values as $a_v_key=>$a_v_v){
						$attr_num = $attr_num+$a_v_v['num'];
					}
					$data['tjMeetingProductList'][$p_key]['num']=$attr_num;
				}

			}
			if($p_v['tag_type']){
				$data['tjMeetingProductList'][$p_key]['tag_ico'] = "/Uploads/type_ico/tag_".$p_v['tag_type'].".jpg";
			}
		}
		//产品
		foreach($iconArr as $key=>$val){
			$map4['a.flag'] = 1;
			$map4['a.is_on'] = 1;
			$map4['a.type_id'] = $val['typeId'];
			if($shop_id){
				$map4['b.shop_id'] = $shop_id;
				$map4['b.flag'] = 1;
			}
			$data['tjProductList'][$key]['typeId'] = $val['typeId'];
			$data['tjProductList'][$key]['typeName'] = $val['typeName'];
			$data['tjProductList'][$key]['icon'] = "/Uploads/icon/".$val['typeId'].".jpg";
			//$data['tjProductList'][$key]['productList'] = (array) $ShopProduct->alias('b')->join('left join __PRODUCT__ as a on a.id = b.product_id')->where($map4)->order("b.seq desc")->field('a.id,name,a.photo,a.price,a.spec,b.num,a.is_attr,a.tag_type')->limit(3)->select();
			$data['tjProductList'][$key]['productList'] = (array) $ShopProduct->alias('b')->join('left join __PRODUCT__ as a on a.id = b.product_id')->where($map4)->order("b.seq desc")->field('a.id,name,a.photo,CASE WHEN  b.shop_price>0 THEN  b.shop_price ELSE a.price END AS price,a.spec,b.num,a.is_attr,a.tag_type')->limit(3)->select();
			//$data['tjProductList'][$key]['productList'] = (array) $Product->where($map4)->field('id,name,photo,price,spec,num,is_attr')->limit(3)->select();
			foreach($data['tjProductList'][$key]['productList'] as $p_key=>$p_v){
				if($p_v['is_attr']==1){
					$attr_where = array();
					$attr_where['product_id'] = $p_v['id'];
					$attr_where['flag'] = 1;
					if(isset($param['shop_id'])){
						$attr_where['shop_id'] = $param['shop_id'];
					}
					$attribute_values = $ShopAttributeValues->field("id,attr_id,attr_name,attr_value_name,is_default,num")->where($attr_where)->order('seq desc')->select();
					if(!empty($attribute_values)){
						$data['tjProductList'][$key]['productList'][$p_key]['attribute_values'] = $attribute_values;
						$attr_num = 0;
						foreach($attribute_values as $a_v_key=>$a_v_v){
							$attr_num = $attr_num+$a_v_v['num'];
						}
						$data['tjProductList'][$key]['productList'][$p_key]['num']=$attr_num;
					}
				}
				if($p_v['tag_type']){
					$data['tjProductList'][$key]['productList'][$p_key]['tag_ico'] = "/Uploads/type_ico/tag_".$p_v['tag_type'].".jpg";
				}
			}
		}
		echo $this->suc($data);
	}

	public function bannerList(){
		$Banner = M('Banner');
		$param = $this->param;
		$data = (array) $Banner->where('flag = 1 and is_on = 1 and type != 5')->field('id,title,img_app as img,type,url')->order('seq asc')->select();
		echo $this->suc($data);
	}
	/**
	 * 区县列表
	 * Enter description here ...
	 */
	public function getArea(){
		$Province = M('ZhProvince');$City = M('ZhCity');$Area = M('ZhArea');
		if(S('S_SYSTEM_AREA')){//启用缓存
			$provinceData = S('S_SYSTEM_AREA');
		}else{
			$provinceData = $Province->select();
			foreach($provinceData as $pk=>$pv){
				$cityData = $City->where('province_id = '.$pv['id'])->field('id as cityId,city')->select();
				$provinceData[$pk]['cityList'] = $cityData;
				foreach ($cityData as $ck=>$cv){
					$areaData = $Area->where('city_id = '.$cv['cityId'])->field('id as areaId,area')->select();
					$provinceData[$pk]['cityList'][$ck]['areaList'] = $areaData;
				}
			}
			S('S_SYSTEM_AREA',$provinceData,2592000);//30天后过期
		}
		echo $this->suc($provinceData);
	}
	
	/**
	 * 获取学校列表
	 */
	public function getSchoolList(){
		$School = M('School');
		if(S('S_SYSTEM_SCHOOL')){
			$data = S('S_SYSTEM_SCHOOL');
		}else{
			$data = $School->where('flag = 1')->field('id,province,city,area,name')->order('seq asc')->select();
			S('S_SYSTEM_SCHOOL',$data,86400);//30天后过期
		}
		echo $this->suc($data, 0);
	}
}